package com.truyentranh.requester;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.graphics.Paint.Join;

import com.truyentranh.data.HaiBLMetalData;
import com.truyentranh.data.HaiBLModule;
import com.truyentranh.data.TamDiemDetailModuleData;
import com.truyentranh.network.Request;
import com.truyentranh.network.RequestListener;
import com.truyentranh.network.UrlHelper;
import com.truyentranh.utils.AppSettingParams;
import com.truyentranh.utils.Logger;

public class GirlRequester extends Request{

	private String method = "";
	private static String keyRandom = "";
	private int page = 1;
	
	private RequestListener requestListener;
	
	//Data
	ArrayList<HaiBLMetalData> haiVLMetalDatas = new ArrayList<HaiBLMetalData>();
	private ArrayList<TamDiemDetailModuleData> tamDiemDetailModuleDataDatas = new ArrayList<TamDiemDetailModuleData>();
	
	public void setMethod(String aMethod){
		this.method = aMethod;
	}
	
	public void setPage(int page){
		this.page = page;
	}
	
	public void setRequestListener(RequestListener requestListener){
		this.requestListener = requestListener;
	}	

	@Override
	public void cancelRequest() {
		// TODO Auto-generated method stub		
	}
	
	public static GirlRequester getDetail(String url, RequestListener requestListener){
		//http://haibl.net/public/girl/detail?url=http%3a%2f%2fwww.depvd.com%2fview%2fEm-Xinh-Tuoi-Choi-Ben-Suoi%2fDP4e7Nqvrj
		//&key=2861010&count=50&page=1&sig=2d5341a9e9a4d76aa10ceb661806ebf1
		GirlRequester girlRequester = new GirlRequester();
		girlRequester.method = url;
		girlRequester.requestListener = requestListener;
		
		return girlRequester;
	}

	@Override
	public void runRequest() {
		if(method.startsWith("http://")){
			url = UrlHelper.getUrlOfMethod(UrlHelper.BASE_URL_DETAIL,
					new String[]{"url","page"               ,"count", "rndk"},
					new String[]{ method , String.valueOf(page),String.valueOf(AppSettingParams.COUNT), GirlRequester.keyRandom});
		} else {
			// TODO Auto-generated method stub
			url = UrlHelper.getUrlOfMethod(UrlHelper.BASE_URL_GIRL,
					new String[]{"method","page"               ,"count", "rndk"},
					new String[]{ method , String.valueOf(page),String.valueOf(AppSettingParams.COUNT), GirlRequester.keyRandom});
		}
		
		send();
	}
	
	public ArrayList<HaiBLMetalData> getDataList(){
		return haiVLMetalDatas;
	}
	
	@Override
	public void requestFinished(JSONObject jsonObject) {
		if(method.startsWith("http://")){
//			TamDiemDetailModuleData
			if(jsonObject.has("data")){
				JSONArray array = null;
				try {
					array = jsonObject.getJSONArray("data");
					
					for(int i = 0 , n = array.length() ; i < n ; i++){
						JSONObject index = array.getJSONObject(i);
						TamDiemDetailModuleData detailModuleData = new TamDiemDetailModuleData();
						//smallsrc
						detailModuleData.bigsrc = index.has("bigsrc") ? index.getString("bigsrc") : "";
						detailModuleData.src = index.has("src") ? index.getString("src") : "";
						detailModuleData.thumbnailsrc = index.has("thumbnailsrc") ? index.getString("thumbnailsrc") : "";
						detailModuleData.avatar = index.has("thumbnailsrc") ? index.getString("thumbnailsrc") : "";
						detailModuleData.smallsrc = index.has("smallsrc") ? index.getString("smallsrc") : "";
						
						getTamDiemDetailModuleDataDatas().add(detailModuleData);
					}
					
					if (requestListener != null) {
						requestListener.onRequestComplete(this);
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		} else {
			if (jsonObject != null && jsonObject.has("rndk")) {
				try {
					GirlRequester.keyRandom = jsonObject.getString("rndk");
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
			if (jsonObject != null && jsonObject.has("data")) {
				try {
					JSONArray jsonArray = jsonObject.getJSONArray("data");
					final int size = jsonArray.length();
					for (int i = 0; i < size; i++) {
						JSONObject datas = jsonArray.getJSONObject(i);

						String imageUrl = datas.has(HaiBLMetalData.SRC) ? datas.getString(HaiBLMetalData.SRC) : "";
						String detailUrl = datas.has(HaiBLMetalData.IMG) ? datas.getString(HaiBLMetalData.IMG) : "";
						String title = datas.has(HaiBLMetalData.TITLE) ? datas.getString(HaiBLMetalData.TITLE) : "";

						String uploader = datas.has(HaiBLMetalData.UPLOADER) ? datas.getString(HaiBLMetalData.UPLOADER) : "";
						String viewCount = datas.has(HaiBLMetalData.VIEW_COUNT) ? datas.getString(HaiBLMetalData.VIEW_COUNT) : "0";
						String commentCount = datas.has(HaiBLMetalData.COMMENT_COUNT) ? datas.getString(HaiBLMetalData.COMMENT_COUNT) : "0";
						String likeCount = datas.has(HaiBLMetalData.LIKE_COUNT) ? datas.getString(HaiBLMetalData.LIKE_COUNT) : "0";
						String bigImageUrl = datas.has(HaiBLMetalData.BIG_SRC) ? datas.getString(HaiBLMetalData.BIG_SRC) : "";
						boolean hasmore = datas.has("more") ? datas.getInt("more") == 1 : false;

						HaiBLMetalData haiBLMetalData = new HaiBLMetalData(imageUrl, detailUrl, title, uploader, viewCount, commentCount);
						haiBLMetalData.setLikeCount(likeCount);
						haiBLMetalData.setBigImageUrl(bigImageUrl);
						haiBLMetalData.setTYPE(HaiBLModule.GIRLXINH);
						haiBLMetalData.setHasMore(hasmore);
						haiVLMetalDatas.add(haiBLMetalData);
					}

					if (requestListener != null) {
						requestListener.onRequestComplete(this);
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}        
	}

	@Override
	public void requestFailed(int errorCode, String message) {
		// TODO Auto-generated method stub
		if(requestListener != null){
			requestListener.onRequestFailed(message, this);
		}
	}

	public ArrayList<TamDiemDetailModuleData> getTamDiemDetailModuleDataDatas() {
		return tamDiemDetailModuleDataDatas;
	}

	public void setTamDiemDetailModuleDataDatas(ArrayList<TamDiemDetailModuleData> tamDiemDetailModuleDataDatas) {
		this.tamDiemDetailModuleDataDatas = tamDiemDetailModuleDataDatas;
	}

}
